package com.example.module.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;

import com.example.entity.Const;

public class CustomWebView extends WebView {
	private static final String LOG_TAG = "CustomWebView";
	
	float prevEventY;
	boolean isForward, isHold;
	public int currScrollY, prevScrollY, scrollMotionY;
	int scrollTension = 0, scrollCount = 0, tensionCount = 5, tensionAverage = 0;
	
	private ScrollListener scrollListener;
	
	public CustomWebView(Context context) {
		super(context);
		initialize();
	}
	public CustomWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize();
	}
	public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize();
	}

	public void initialize() {
		requestFocus(View.FOCUS_DOWN);
		setHorizontalScrollBarEnabled(false);
		setVerticalScrollBarEnabled(true);
		setVerticalScrollbarOverlay(true);
		
		WebSettings webSettings = this.getSettings();
		if (webSettings != null) {
			webSettings.setRenderPriority(RenderPriority.HIGH);
			webSettings.setLoadsImagesAutomatically(true);
			webSettings.setBuiltInZoomControls(true); 
			webSettings.setSupportZoom(true); //상세 웹뷰에서만 사용
			webSettings.setJavaScriptEnabled(true);
			webSettings.setLoadWithOverviewMode(true);
			webSettings.setUseWideViewPort(true);

			webSettings.setDomStorageEnabled(true);
			webSettings.setDatabaseEnabled(true);
			webSettings.setDatabasePath(Const.WEB_DATABASE_PATH);
			webSettings.setAllowFileAccess(true);
			webSettings.setSupportMultipleWindows(false);
			webSettings.setAppCacheEnabled(true);
			webSettings.setAppCachePath(Const.WEB_CACHE_PATH);
			webSettings.setAppCacheMaxSize(10 * 1024 * 1024);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (scrollListener != null) {
			final int action = ev.getAction();
			final float eventY = ev.getY();
			final int motionY = Math.abs((int) (eventY - prevEventY));
			final int scrollY = getScrollY();
			
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				isHold = false;
				Log.e(LOG_TAG, "ACTION_DOWN isHold=" + isHold + " motionY=" + motionY + " scrollY=" + scrollY);
				break;
				
			case MotionEvent.ACTION_MOVE:
				isHold = true;
				isForward = eventY - prevEventY > 0 ? false : true;
				
				if (isHold && !isForward && scrollY == 0 && scrollListener != null) {
					scrollListener.onScrollTo(motionY);
				}
				
				Log.e(LOG_TAG, "ACTION_MOVE isHold=" + isHold + " isForward=" + isForward + " motionY=" + motionY + " scrollY=" + scrollY);
				break;
				
			case MotionEvent.ACTION_UP:
				isHold = false;
				Log.e(LOG_TAG, "ACTION_UP isHold=" + isHold + " motionY=" + motionY + " scrollY=" + scrollY);
				break;
				
			default:
				break;
			}
			
			prevEventY = eventY;
		}
		
		return super.dispatchTouchEvent(ev);
	}
	
	public interface ScrollListener {
		public void onScrollTo(int distance);
		public void onScrollBy(int distance);
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		if (scrollListener != null) {
			currScrollY = t;
			prevScrollY = oldt;
			scrollMotionY = Math.abs(oldt - t);
			Log.e(LOG_TAG, "onScrollChanged isHold= " + isHold + " prevScrollY=" + prevScrollY + " scrollY=" + currScrollY + " scrollMotionY=" + scrollMotionY);
			
			if (scrollCount < tensionCount) {
				scrollTension += scrollMotionY;
				scrollCount++;
			}
			else {
				tensionAverage = scrollTension / tensionCount;
				Log.e(LOG_TAG, "Tension AVG=" + tensionAverage);
				scrollTension = 0;
				scrollCount = 0;
			}
			
			if (!isHold && !isForward && currScrollY == 0) {
				scrollListener.onScrollBy(tensionAverage);
			}
		}
		super.onScrollChanged(l, t, oldl, oldt);
	}

	public ScrollListener getScrollListener() {
		return scrollListener;
	}
	public void setScrollListener(ScrollListener scrollListener) {
		this.scrollListener = scrollListener;
	}
}
